load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/install:install_data.bzl", "install_data")
load(
    "//tools/skylark:drake_cc.bzl",
    "drake_cc_googletest",
)
load("//tools/workspace:forward_files.bzl", "forward_files")
load(
    "//tools/workspace/intel_realsense_ros_internal:files.bzl",
    "realsense2_description_files",
)

package(default_visibility = ["//visibility:public"])

# TODO(jwnimmer-tri) Convert this into drake_models somehow or another.
_REALSENSE2_DESCRIPTION_FILES = forward_files(
    srcs = ["@intel_realsense_ros_internal//:" +
            x for x in realsense2_description_files()],
    dest_prefix = "",
    strip_prefix = "@intel_realsense_ros_internal//:realsense2_description/",
    tags = ["manual"],
    visibility = ["//visibility:private"],
)

_STLS = [
    x
    for x in _REALSENSE2_DESCRIPTION_FILES
    if x.endswith(".stl")
]

_OBJS = [x[:-3] + "obj" for x in _STLS]

[
    # Recall this about genrules:
    #
    # - `srcs` are built for the "target" configuration, which is what all of
    #   our other programs and tests use; these will have a nice warm cache.
    #
    # - `tools` are built for the "exec" configuration, which is only ever used
    #   for genrules (or `tools` for any other flavor of rule); any large
    #   programs here will be costly to rebuild completely from scratch just
    #   for the genrule.
    #
    # Therefore, we add `stl2obj` as `srcs` (not `tools`) to avoid building it
    # in the "exec" configuration all by its lonesome self. We want to use the
    # same build of its dependencies as all of our regular code, for a smaller
    # footprint and better caching.
    genrule(
        name = obj + "_genrule",
        srcs = [stl] + ["//manipulation/util:stl2obj"],
        outs = [obj],
        cmd = " ".join([
            "env TSAN_OPTIONS=report_bugs=0",
            "$(location //manipulation/util:stl2obj)",
            "--input=$(location {})".format(stl),
            "--output=$@",
            "--reduction=0.98",
        ]),
        visibility = ["//visibility:private"],
    )
    for stl, obj in zip(_STLS, _OBJS)
]

filegroup(
    name = "models",
    srcs = [
        x
        for x in _REALSENSE2_DESCRIPTION_FILES + _OBJS
        if x not in _STLS
    ],
)

install_data(
    name = "install_data",
    data = [":models"],
)

drake_cc_googletest(
    name = "realsense_parse_test",
    data = [":models"],
    deps = [
        "//common:find_resource",
        "//geometry:scene_graph",
        "//multibody/parsing",
        "//systems/analysis:simulator",
        "//systems/framework:diagram_builder",
    ],
)

add_lint_tests()
